package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SalaryCycleAddDTO;
import org.jsola.hr.dto.SalaryCycleUpdateDTO;
import org.jsola.hr.entity.SalaryCycleDO;
import org.jsola.hr.query.SalaryCycleQuery;
import org.jsola.hr.service.ISalaryCycleService;
import org.jsola.hr.vo.SalaryCycleListVO;
import org.jsola.hr.vo.SalaryCycleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrSalaryCycleController")
@RequestMapping(HrConstants.API_PATH + "/v1/salaryCycles")
public class SalaryCycleController implements ISalaryCycleControllerAPI {

    @Autowired
    private ISalaryCycleService salaryCycleService;

    @Override
    @PostMapping
    public Result<SalaryCycleVO> save(@Valid @RequestBody SalaryCycleAddDTO salaryCycleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增计薪周期设置权限校验

        return Result.success(
                salaryCycleService.save(salaryCycleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SalaryCycleUpdateDTO salaryCycleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改计薪周期设置权限校验

        int count = salaryCycleService.update(salaryCycleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{salaryCycleId}")
    public Result<Integer> delete(@PathVariable String salaryCycleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除计薪周期设置权限校验

        int count = salaryCycleService.deleteByIds(tokenUser, salaryCycleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] salaryCycleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除计薪周期设置权限校验

        int count = salaryCycleService.deleteByIds(tokenUser, salaryCycleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{salaryCycleId}")
    public Result<SalaryCycleVO> findById(@PathVariable String salaryCycleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看计薪周期设置权限校验

        return Result.success(
            salaryCycleService.selectById(salaryCycleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SalaryCycleListVO>> listAll(@Valid SalaryCycleQuery salaryCycleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看计薪周期设置权限校验

        return Result.success(
            salaryCycleService.select(salaryCycleQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SalaryCycleListVO>> page(@Valid SalaryCycleQuery salaryCycleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            salaryCycleService.selectPage(salaryCycleQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "saveOrUpdate")
    public Result<SalaryCycleVO> saveOrUpdate(@Valid @RequestBody SalaryCycleUpdateDTO salaryCycleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改计薪周期设置权限校验

        return Result.success(
                salaryCycleService.saveOrUpdate(salaryCycleUpdateDTO, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectDOBySiteId")
    public Result<SalaryCycleVO> selectDOBySiteId() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看计薪周期设置权限校验

        SalaryCycleDO salaryCycleDO = salaryCycleService.selectDOBySiteId(tokenUser.getSiteId());
        return Result.success(
                salaryCycleDO == null ? null : salaryCycleDO.to(SalaryCycleVO.class)
        );
    }


}

